
export default {
    methods: {
        handleParams(page = this.page, opts = this.form){
            const params = {
                currentStatus: 0, //1 待处理 2 已处理  3 已达成  4 已失效
                phone: '',
                realName: '',
                batchCode: '',
                distributionTimeStart: '',
                distributionTimeEnd: '',
                salesResult: '',
                recyclingStatus: '',
                salesmanName: '',
                salesmanGroupName: '',
                marketingType: '',
                appName: '',
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                }
            };
            if (opts) {
                Object.keys(params).forEach(key => {
                    if (opts[key]) {
                        params[key] = opts[key];
                    }
                });
            }
            const { distributionTime, nextFollowTime, agreeTime, invalidTime, salesResultRecyclyStatus } = this.form;
            if (distributionTime && distributionTime.length) {
                params.distributionTimeStart = distributionTime[0];
                params.distributionTimeEnd = distributionTime[1];
            }
            // 已处理
            if (nextFollowTime && nextFollowTime.length) {
                params.nextFollowTimeStart = nextFollowTime[0];
                params.nextFollowTimeEnd = nextFollowTime[1];
            }
            // 已达成
            if (agreeTime && agreeTime.length) {
                params.agreeTimeStart = agreeTime[0];
                params.agreeTimeEnd = agreeTime[1];
            }
            // 已失效
            if (invalidTime && invalidTime.length) {
                params.invalidTimeStart = invalidTime[0];
                params.invalidTimeEnd = invalidTime[1];
            }
            // 已失效
            if(salesResultRecyclyStatus===1){
                params.recyclingStatus = salesResultRecyclyStatus;
            }else if(salesResultRecyclyStatus===2){
                params.salesResult = salesResultRecyclyStatus;
            }
            return params;
        },
        getTableData(page = this.page, opts = this.form) {
            const params = this.handleParams(page,opts);
            if(params.batchCode&&typeof(params.batchCode)!='string'){
                if(params.batchCode.length>0){
                    params.batchCode = params.batchCode.join(',')
                }else{
                    params.batchCode=''
                }
            };
            this.$api.telSales.queryUserList(params).then(res => {
                const { status, data, error } = res;
                if (status === "1" && error === "00000000") {
                    this.tableData = data.data || [];
                    this.page.recordsTotal = data.pageCount;
                    this.totalRecord = data.pageCount;
                    this.page.currentPage = page.currentPage;
                    let searchPanelHeight = 270;
                    const { currentStatus } = this.form;
                    if (1 === currentStatus || (4 === currentStatus && this.roleId === 3)) {
                        searchPanelHeight = 330;
                    }
                    this.tableMaxHeight = window.document.body.clientHeight - searchPanelHeight;
                }
            });
        },
        search() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.getTableData({ currentPage: 1 }, this.form);
                    const { currentStatus } = this.form;
                    if ([1, 2].includes(currentStatus))
                    this.$storage.set('willDetail',this.form);
                }
            });
        },
        exportExcel() {
            const params = this.handleParams();
            // const { phone, realName, batchCode, distributionTimeStart, distributionTimeEnd, agreeTimeStart, agreeTimeEnd, salesmanName, salesmanGroupName, appName, marketingType } = params;
            // if(!phone && !realName && !batchCode && !distributionTimeStart && !distributionTimeEnd
            //     && !agreeTimeStart && !agreeTimeEnd && !salesmanName && !salesmanGroupName && !appName && !marketingType) {
            //     this.$message.error(this.$tc('verify.plsCheckFilters'));
            //     return;
            // }
            let check = false;
            Object.keys(params).forEach(key => {
                if (key !== 'page' && key !== 'currentStatus' && params[key]) {
                    check = true;
                }
            });
            if (!check) {
                this.$message.error(this.$tc('verify.plsCheckFilters'));
                return;
            };
            if(params.batchCode){
                params.batchCode = params.batchCode.join(',')
            }
            this.$api.telSales.queryUserList(params).then(res => {
                const { status, data, error } = res;
                if (status === "1" && error === "00000000") {
                    if( data.pageCount > 5000 ){
                        //  if( data.pageCount > 5 ){//测试改为5条
                        this.$message.warning(this.$tc('salesManage.tipsMax'));
                        //当超过500条给予提示，并重置导出按钮开关
                        //this.$emit('updateWillExport');
                    }else{
                        delete params.page;
                        this.$exportExcel.telSales.exportAgreeUserList(params);
                    }
                }
            });
        },
        goDetail(row, rowIndex) {
            let nextIndex = (this.page.currentPage - 1) * this.page.pageSize + rowIndex + 2;
            console.warn(row)
            if(row.register){
                this.$openNewBlank(
                    `#/conf/telemarketing/tel-manage/details/${row.userId}/${this.$root.local}/${row.telUserId}?currentStatus=${row.currentStatus}&batchCode=${row.batchCode}&id=${row.id}&nextIndex=${nextIndex}&register=${row.register?1:false}&domain=${row.domain}&phone=${row.phone}&company=${row.company}&appName=${row.appName}&remark=${row.remark?row.remark:"--"}`
                );
            }else{
                this.$openNewBlank(
                    `#/conf/telemarketing/tel-manage/details/${row.userId}/${this.$root.local}/${row.telUserId}?currentStatus=${row.currentStatus}&batchCode=${row.batchCode}&id=${row.id}&nextIndex=${nextIndex}&phone=${row.phone}&appName=${row.appName}&remark=${row.remark?row.remark:"--"}`
                )
                }
        },
        getUserInfo() {
            this.$api.telSales.getUserInfo({ userId: this.userStorage.id }).then(res => {
                const { status, error, data } = res;
                if (status === "1" && error === "00000000") {
                    this.roleId = data.roleId;
                    this.getTableData({ currentPage: this.page.currentPage });
                }
            });
        },
        getCaptainList() {
            this.$api.telSales.getAllUserInfo({ roleId: 2 }).then(res => {
                const { status, error, data } = res;
                if (status === "1" && error === "00000000") {
                    this.captainList = data.data;
                }
            });
        },
    },
};
